<template>
	<view class="confirmOrder">
		<view class="top" v-if="info.type == 1">
			<view v-if="info.address.id > 0" class="address" @click="jump('address', optionsParam)">
				<image :src="obtainImgUrl('icon/icon33.png')" mode="aspectFill"></image>
				<view class="right">
					<view class="txt">
						<text>收货人：{{info.address.realname}}</text>
						<text>{{info.address.mobile}}</text>
					</view>
					<view class="txt1">
						收货地址：{{info.address.address_name}}{{info.address.address_details}}
					</view>
					<view class="jt"></view>
				</view>
			</view>
			<view v-else class="noaddress" @click="jump('addOreditAddress', 'id=0&formType=1&' +optionsParam)">
				<image :src="obtainImgUrl('icon/noaddress.png')" mode="widthFix"></image>
				<text>+新建地址</text>
			</view>
			<image class="img" :src="obtainImgUrl('icon/icon34.png')" mode="aspectFill"></image>
		</view>
		<view class="main">
			<view class="goods">
				<image :src="info.picture" mode="aspectFill"></image>
				<view class="right">
					<text>{{info.name}}</text>
					<view class="size">
						<text>{{info.num}} 张</text>
					</view>
					<text class="num">{{'￥'+info.price}}</text>
				</view>
			</view>
		</view>
		<view class="line">
			<text>总金额</text>
			<text>{{'￥'+info.totalMoney}}</text>
		</view>
		<view class="common"  v-if="info.type == 1">
			<text>买家留言</text>
			<textarea maxlength="500" placeholder-style="color:rgba(66,81,114,0.2);" value="formData.buyer_message"
				placeholder="留言备注..." v-model="formData.buyer_message" />
		</view>
       <view class="bottom" style="z-index: 10;">
		   <view class="btn" @click="createOrder()">确定购买</view>
		</view>
		<uni-popup ref="popup" type="bottom">
			<view class="pay_type">
				<image @click="close" class="close" :src="obtainImgUrl('icon/close.png')" mode="aspectFill"></image>
				<view class="title">选择支付方式</view>
				<view class="paypir">
					<text>应付金额</text>
					<text>{{'￥'+payFormData.payMoney}}</text>
				</view>
				<view class="line2" v-if="info.user_money > 0"  @click="selUseMoney()">
					<text>余额支付 - 当前余额：{{info.user_money}}</text>
					<view :class="formData.isUseMoney ?'sel':'nosel'" ></view>
				</view>
				<view class="line2" v-if="payFormData.payMoney > 0">
					<text>在线微信支付</text>
					<image :src="obtainImgUrl('icon/icon37.png')" mode="aspectFill"></image>
				</view>
				<view class="pay_bot" @click="subPay()">确认支付</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import serve from '../../static/request.js';
	import storage from '../../static/appkey.js';
	import common from '../../static/jump.js';
	export default {
		data() {
			return {
				info: {},
				formData: {
					address_id: 0,
					cardId: 0,
					num : '',
					isUseMoney: 0,
				},
				payFormData: {
					id: 0,
					type: '',
					payMoney: 0,
					isUseMoney: 0,
				},
				optionsParam: '',
				check_sub: false,
			}
		},
		methods: {
			resetAddress(str) {
				this.formData.address_id = str.id;
				this.info.address = str;
				this.$forceUpdate();
			},
			previewOrder() {
				serve.request({
					url: '/store/order/receive',
					data: this.formData
				}).then(res => {
					if(res.code != 200) {
						uni.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
							success:function(){
								common.jump('return_one');
							}
						});
						return;
					}
					this.info = res.data;
					this.payFormData.payMoney = JSON.parse(JSON.stringify(this.info.totalMoney));
					this.formData.address_id = this.info.address.id;
					this.$forceUpdate();
				});
			},
			numFilter(value) {
				let tempVal = parseFloat(value).toFixed(3);
				let realVal = tempVal.substring(0, tempVal.length - 1);
				return realVal;
			},
			close() {
				this.$refs.popup.close();
			},
			createOrder() {
				this.$refs.popup.open();
			},
			selUseMoney() {
				this.payFormData.payMoney = JSON.parse(JSON.stringify(this.info.totalMoney));
				this.formData.isUseMoney = this.formData.isUseMoney == 1 ? 0 : 1;
				this.payFormData.isUseMoney = this.formData.isUseMoney;
				if(this.formData.isUseMoney) {
					this.payFormData.payMoney = this.numFilter(Number(this.payFormData.payMoney) - Number(this.info.user_money));
				}
				this.payFormData.payMoney = this.payFormData.payMoney > 0 ? this.payFormData.payMoney : 0;
			},
			subPay() {
				if(this.info.type == 1 && this.formData.address_id == 0) {
					uni.showToast({
						title: "请选择收货地址",
						icon: 'none'
					})
					return;
				}
				if(this.check_sub == true) {
					return ;
				}
				if(this.payFormData.id > 0) {
					this.wechatPay();
					return ;
				}
				uni.showLoading({
					title: '提交中···',
					mask: true
				});
                let _self = this;
				_self.check_sub = true;
				serve.request({
                    url: '/store/order/sub-receive',
                    data: _self.formData
                }).then(res => {
                    uni.hideLoading();
					_self.check_sub = false;
                    if(res.code != 200) {
						uni.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
						});
						return;
                    } 
					_self.payFormData.type = res.data.type;
					_self.payFormData.id = res.data.id;
					if(res.data.isPay) {
						_self.wechatPay();
						return ;
					}
					common.jump('success', 'type='+_self.payFormData.type+'&id='+_self.payFormData.id);
                    return ;
                });
			},
			wechatPay(){
				var _self = this;
				serve.request({
                    url: '/store/pay/wechat',
                    data: _self.payFormData
                }).then(res => {
                    if(res.code != 200) {
                        uni.showToast({
                            title:res.message,
                            icon: 'none'
                        });
                        return ;
                    }
                    let payConfig = res.data;
                    uni.requestPayment({
                        provider: 'wxpay',
                        timeStamp: payConfig.timeStamp,
                        nonceStr: payConfig.nonceStr,
                        package: payConfig.package,
                        signType: payConfig.signType,
                        paySign: payConfig.paySign,
                        success: function (res) {
                            _self.queryPayResult(payConfig.payId);
                            return ;
                        },
                        fail: function (err) {
                            uni.showToast({
                                title: '放弃支付',
                                icon: 'none'
                            });
                            common.jump('success', 'type=create_order&id='+_self.payFormData.id);
                            return ;
                        }
                    })
                });
				return ;
			},
			queryPayResult(payId) {
				uni.showLoading({
					title: '查询中···',
					mask: true
				});
				var self = this;
				setTimeout(function() {
					serve.request({
						url: '/payservice/query-pay',
						data:{
							payId: payId,
						}
					}).then(res => {
						if(res.data.payStatus == 1) {
							self.jump('success', 'type=pay_order&id='+res.data.dataId);
						} else {
							self.queryPayResult(payId);
						}
					});
				}, 1500);
			},
			obtainImgUrl(imgUrl) {
				return serve.imgUrl()+imgUrl;
			},
			jump(jump_type, jump_link){
				common.jump(jump_type, jump_link);
			},
		},
		onLoad(options) {
			if(uni.getStorageSync('access-token') == '') {
				common.jump('login');
				return ;
			}
			this.formData.cardId = options.cardId || 0;
			this.formData.num = options.num || 0;
			this.optionsParam = 'cardId=' + this.formData.cardId + '&num=' + this.formData.num;
			this.previewOrder();
		},
		onPullDownRefresh() {
			console.log('refresh');
			this.previewOrder();
		}
	}
</script>

<style scoped lang="scss">
	@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
		    .confirmOrder {
		        padding-bottom: 124rpx !important;
				.bottom{
					 padding-bottom: 12px !important;
				}
		    }
	}
	.confirmOrder {
		padding-bottom: 100rpx;
        .bottom{
			 z-index: 100;
			 position: fixed;
			 width: 100%;
			 bottom: 0rpx;
			 border-top: 1rpx rgba(235,236,240,1) solid;
			 align-items: center;
			 // display: flex;
			 box-sizing: border-box;
			 height:120rpx;
			 background:rgba(255,255,255,1);
			 box-shadow:0px 1px 0px 0rpx rgba(235,236,240,1);
			 padding-left: 30rpx;
			 font-size:24rpx;
			 color:rgba(60,72,104,0.5);
			 padding-bottom: env(safe-area-inset-bottom);
			 .btn{
				 position: absolute;
				 height: 100%;
				 width: 100%;
				 background:rgba(235,89,56,1);
				 line-height:100rpx ;
				 text-align: center;
				 font-size:28rpx;
                 font-weight:bold;
                 color:rgba(255,255,255,1);
				 right: 0rpx;
			 }
		 }
		 .top {
		 	position: relative;
		 	border-bottom: 16upx rgba(251, 251, 253, 1) solid;
		 
		 	.noaddress {
		 		height: 190upx;
		 		width: 100%;
		 		display: flex;
		 		justify-content: center;
		 		align-items: center;
		 
		 		image {
		 			width: 40upx;
		 		}
		 
		 		text {
		 			font-size: 32upx;
		 			font-weight: bold;
		 			color: rgba(235, 89, 56, 1);
		 			margin-left: 10upx;
		 		}
		 	}
		 
		 	.address {
		 		display: flex;
		 		position: relative;
		 		padding: 53upx 70upx;
		 		font-size: 28upx;
		 		font-weight: bold;
		 		align-items: center;
		 
		 		image {
		 			position: absolute;
		 			left: 30upx;
		 			width: 31upx;
		 			height: 37upx;
		 			top: 120upx;
		 		}
		 
		 		.txt {
		 			display: flex;
		 			justify-content: space-between;
		 			padding-bottom: 28upx;
		 		}
		 
		 		.jt {
		 			position: absolute;
		 			right: 30upx;
		 			top: 105upx;
		 			content: '';
		 			border: 1upx solid rgba(57, 72, 107, 0.5);
		 			border-width: 0 3upx 3upx 0;
		 			padding: 6upx;
		 			transform: rotate(-45deg);
		 			-webkit-transform: rotate(-45deg);
		 		}
		 	}
		 
		 	.img {
		 		position: absolute;
		 		bottom: 0;
		 		height: 10upx;
		 		width: 100%;
		 	}
		 }
		.main {
			width: 100%;
		
			.goods {
				box-sizing: border-box;
				display: flex;
				align-items: center;
				padding: 40rpx 30rpx;
				border-bottom: 1rpx rgba(235, 236, 240, 1) solid;

				image {
					width: 170rpx;
					height: 170rpx;
					margin-right: 20rpx;
				}

				.right {
					position: relative;
					width: 503rpx;
					display: flex;
					justify-content: center;
					flex-direction: column;
					font-size: 32rpx;

					.operate {
						position: absolute;
						right: 0rpx;
						bottom: 0rpx;
						width: 130rpx;
						height: 48rpx;
						border: 1px solid rgba(57, 72, 107, 1);
						border-radius: 24rpx;
						text-align: center;
						font-size: 24rpx;
						line-height: 48rpx;
						font-weight: bold;
						color: rgba(60, 72, 104, 0.5);
					}

					.size {
						display: flex;
						justify-content: space-between;
						padding: 26rpx 0rpx;
						color: rgba(60, 72, 104, 0.5);
						font-size: 24rpx;
						align-items: center;
					}

					text:first-child {
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}

					.num {
						font-size: 24rpx;
						color: rgba(255, 198, 0, 1);
					}
				}
			}

		}
		.line {
			box-sizing: border-box;
			padding: 37rpx 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 28rpx;
			font-weight: bold;
			color: rgba(60, 72, 104, 0.8);
			border-bottom: 1rpx rgba(235, 236, 240, 1) solid;

			text:last-child {
				color: rgba(255, 198, 0, 1);
				font-size: 32rpx;
			}

			.jt {
				position: absolute;
				right: 20rpx;
				top: 50rpx;
				content: '';
				border: 1rpx solid rgba(57, 72, 107, 0.5);
				border-width: 0 3rpx 3rpx 0;
				padding: 6rpx;
				transform: rotate(-45deg);
				-webkit-transform: rotate(-45deg);
			}
			
		}

		.pic {
			box-sizing: border-box;
			padding: 37rpx 30rpx;
			display: flex;
			justify-content: flex-end;
			font-size: 20rpx;
			color: rgba(60, 72, 104, 1);
			border-bottom: 1rpx rgba(235, 236, 240, 1) solid;

			text:last-child {
				color: rgba(255, 198, 0, 1);
				font-size: 28rpx;
			}

			;
		}

		.common {
				box-sizing: border-box;
				padding: 37rpx 30rpx;
				display: flex;
				flex-direction: column;
				flex-wrap: wrap;
				align-items: flex-start;
				font-weight: bold;
				font-size: 28rpx;
				color: rgba(60, 72, 104, 0.8);
				textarea{
					position: relative;
					z-index: 10;
					font-size:28rpx;
					margin-top: 34rpx;
					padding: 24rpx 28rpx;
					height:92rpx;
					background:rgba(252,252,253,1);
					border-radius:12rpx;
				}
			}
		
		.pay_type{
			position: relative;
			z-index: 200;
			width: 100%;
			background: #FFFFFF;
			padding-bottom: 140rpx;
			.close{
				position: absolute;
				width:23rpx;
				height:23rpx;
				right: 40rpx;
				top: 45rpx;
			}
			.title{
				width: 100%;
				text-align: center;
				height: 120rpx;
				line-height: 100rpx;
				font-size: 32rpx;
				border-bottom: 1rpx rgba(251, 251, 253, 1) solid;
			}
			.paypir{
				display: flex;
				justify-content: center;
				flex-direction: column;
				align-items: center;
				padding: 50rpx 0rpx 20rpx;
				font-size: 26rpx;
				text:last-child{
					color: rgba(255, 200, 10, 1);
					font-size: 32rpx;
					font-weight: bold;
					margin-top: 10rpx;
				}
			}
			.line2{
				width: 100%;
				box-sizing: border-box;
				padding: 30rpx;
				display: flex;
				justify-content: space-between;
				font-size: 28rpx;
				border-bottom: 1rpx rgba(251, 251, 253, 1) solid;
				image{
					width: 40rpx;
					height: 40rpx;
				}
			}
			.nosel {
			    width: 36upx;
			    height: 36upx;
			    border: 1upx solid rgba(60, 72, 104, 1);
			    border-radius: 50%;
			}
			
			.sel {
			    background-image: url(../../static/img/check.png);
			    background-repeat: no-repeat;
			    width: 40upx;
			    height: 40upx;
			    background-size: 100%;
			}
			.pay_bot {
			  width: 100%;
			  position: fixed;
			  left: 0;
			  bottom: 0;
			  height: 100rpx;
			  background: rgba(255, 200, 10, 1);
			  color: #fff;
			  font-size: 32rpx;
			  line-height: 100rpx;
			  text-align: center;
			  border-radius: 0;
			  &::after{
				  border: none;
			  }
			}
		}
	}
</style>
